package com.xdkj.controller;

import com.aliyun.oss.OSSClient;
import com.xdkj.util.OssClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.UUID;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2021/11/29 9:51
 */
@Controller
public class FirstController {
    private OSSClient ossClient = OssClientUtil.getOssClient();
    @RequestMapping("upload")
    @ResponseBody
    public String upload(MultipartFile multipartFile) throws IOException {
        //文件名称
        String name = multipartFile.getOriginalFilename();
        //新名称
        String newName= LocalDate.now().toString()+ UUID.randomUUID().toString()+name;
        ossClient.putObject(OssClientUtil.BUCKETNAME,newName,multipartFile.getInputStream());
        //新url
        String newURL="https://"+OssClientUtil.BUCKETURL+"/"+newName;
        ossClient.shutdown();
        return newURL;
    }
}
